package com.minibobbo.utilities.spe.movementmodules;

public class SpeMoveWalkRunJump extends SpeMovementModule {

	private float walkSpeed = 8.0f;

	public SpeMoveWalkRunJump setWalkSpeed(float walkSpeed) {
		this.walkSpeed = walkSpeed;
		return this;
	}

	public SpeMoveWalkRunJump setRunSpeed(float runSpeed) {
		this.runSpeed = runSpeed;
		return this;
	}

	public SpeMoveWalkRunJump setJumpStrength(float jumpStrength) {
		this.jumpStrength = jumpStrength;
		return this;
	}

	private float runSpeed = 15.0f;
	private float jumpStrength = 10.0f;
	private boolean run = false;
	public int runButton = -1;
	public int jumpButton = -1;
	private float stopAccel = 20;
	// Acceleration in units/second.
	private float acceleration = 12;

	@Override
	public void updateMovement(float delta) {
		if (parent.isButtonPressed(runButton))
			run = true;
		else
			run = false;
		if (parent.l)
			parent.changeVector.x -= acceleration * delta;
		if (parent.r)
			parent.changeVector.x += acceleration * delta;
		// Check for maximum run speeds.
		if (run) {
			if (parent.changeVector.x > runSpeed)
				parent.changeVector.x = runSpeed;
			else if (parent.changeVector.x < -runSpeed)
				parent.changeVector.x = -runSpeed;
			// Check for maximum walk speeds.
		} else {
			if (parent.changeVector.x > walkSpeed)
				parent.changeVector.x = walkSpeed;
			else if (parent.changeVector.x < -walkSpeed)
				parent.changeVector.x = -walkSpeed;

		}

		// If we are on the ground we need to stop quicker

		if (parent.isButtonPressed(jumpButton) && parent.tD)
			parent.physicsVector.y += jumpStrength;
		// If we are not continuing in the same direction, slow down.
		if (((!parent.l && parent.changeVector.x < 0) || (!parent.r && parent.changeVector.x > 0))
				&& parent.tD) {
			// If we are moving left, make this -1
			int change = 1;
			if (parent.changeVector.x < 0)
				change = -1;
			// Slow the object down by the same rate we accelerate.
			parent.changeVector.x = (Math.abs(parent.changeVector.x) - (stopAccel * delta))
					* change;

			if (parent.changeVector.x < .5f && parent.changeVector.x > -.5f)
				parent.changeVector.x = 0;

		}

		if (parent.changeVector.x > 0)
			parent.facing.x = 1;
		else if (parent.changeVector.x < 0)
			parent.facing.x = -1;

	}

	public SpeMoveWalkRunJump setRunButton(int button) {
		runButton = button;
		return this;
	}

	public SpeMoveWalkRunJump setJumpButton(int button) {
		jumpButton = button;
		return this;
	}

}
